#include "cardplayer.h"

CardPlayer::CardPlayer()
{
	cards = new vector<Card>;
}

CardPlayer::CardPlayer( const int& Size )
{
	this->size = Size ;
	cards = new vector<Card>;
	for( unsigned int i = 0 ; i < size ; ++i )
		this->cards->push_back( Card() ) ;
}

vector<Card>& CardPlayer::SortCard() 
{
	if( !Sort() )
	{
		for(  int i= Amount() - 1 ; i > -1 ; --i )
			for(  int  j = 0 ; j < i ; ++j )
				if( cards->at(j) > cards->at( j + 1 ) )
					cards->at(j).Swap( cards->at( j + 1 ) ) ;
	}
	return *( this->cards ) ;
}

bool CardPlayer::Sort() const
{
	bool sort = true ;
	for( int i = 0 ; i < Amount() -1 ; ++i )
		sort &= ( cards->at( i ) < cards->at( i + 1 ) ) ;
	return sort ;
}

bool CardPlayer::Empty() const 
{
	return cards->empty() ;
}

int  CardPlayer::Amount() const
{
	unsigned int i = 0 ;
	Card card;
	for( i ; i < this->Size() ; ++i )
		if( this->cards->at( i ) == card )
			i = i - 1 ;
	return i ;
}

unsigned int CardPlayer::Size() const 
{
	return this->size;
}

bool CardPlayer::Get( CardBox& obj ) 
{
	if( obj.Amount() >= this->Size() )
	{
		for(unsigned int  i = 0 ; i < this->Size() ;  ++i )
			obj.Send( this->cards->at( i ) ) ;
		return true ;
	}
	return false ;
}

Card& CardPlayer::At( const unsigned int& pos ) 
{
	if( pos >= 0 && pos < this->Size() )
		return this->cards->at( pos ) ;
	return Card() ;
}

bool CardPlayer::Remove( const unsigned int& pos )
{
	if( pos >= 0 && pos < this->Size() )
	for( vector<Card>::iterator Iterator = cards->begin() ; Iterator !=  cards->end(); ++Iterator) 
	{ 
		if( *Iterator ==  cards->at( pos ) ) 
		{ 
			 cards->erase( Iterator ) ;
			return true ;
		} 
	}
	return false ;
}

bool CardPlayer::Remove( Card& obj )
{
	for( vector<Card>::iterator Iterator = this->cards->begin() ; Iterator != this->cards->end(); ++Iterator) 
	{ 
		if( *Iterator == obj ) 
		{ 
			this->cards->erase( Iterator ) ;
			return true ;
		} 
	}
	return false ;
}

vector<Card>& CardPlayer::Cards() 
{
	return *( this->cards ) ;
}

void CardPlayer::Assign( vector<Card>& obj )
{
	this->Cards().swap( obj );
}